#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/10/8 16:16
# @Author  : AshJo
# @Desc    : 多线程模块
# @Filename: threading_demo.py
# @Version : v1.0
"""
import os
import time
import random
from threading import Thread, current_thread, active_count, enumerate


# 使用面向对象的方式开启线程

class MyThread(Thread):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        super(MyThread, self).__init__()

    def run(self):
        print(self.ident)


def func(i):
    print(f'start-->{i}', current_thread().ident)  # 输出当前执行的线程id
    time.sleep(random.random())
    print(f'end-->{i}')


if __name__ == '__main__':
    t_l = []
    for i in range(10):
        t = Thread(target=func, args=(i,))
        t.start()
        print(t.ident, os.getpid())
        t_l.append(t)
    print(active_count())  # 显示已启用的线程的个数
    print(enumerate())  # 显示已启用的线程对象 列表
    for t in t_l:
        t.join()
    t = MyThread(1, 2)
    t.start()  # 开启线程后才在线程中执行run()方法
    t.run()
    print(t.ident)
    print('所有线程都执行完了！')
